<template>
  <div>
    <!-- 表头 -->
    <van-nav-bar
      v-show="flag"
      left-text="取消"
      right-text="完成"
      @click-left="onClickLeft"
      @click-right="onClickRight"
    />
    <div>
      <van-button v-show="!flag" @click="flag = true" type="primary"
        >添加</van-button
      >
    </div>

    <!-- 我的应用 -->
    <div  v-show="flag">
      <h3>我的应用</h3>
      <ul class="oul">
        <li v-for="(item, index) in icon" v-show="item.my" :key="index">
          <van-icon :name="item.icon" />
          <p>
            {{ item.icon }}
          </p>
          <span class="close" @click="changFlag(item.my, index)">x</span>
        </li>
      </ul>

      <h3>全部应用</h3>
      <ul class="oul">
        <li v-for="(item, index) in icon" v-show="!item.my" :key="index">
          <van-icon :name="item.icon" />
          <p>
            {{ item.icon }}
          </p>
          <span class="add" @click="changFlag(item.my, index)">+</span>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";
export default {
  data() {
    return {
      flag: false,
      oldIcon: []
    };
  },
  created() {
    this.oldIcon = JSON.parse(JSON.stringify(this.icon))
  },
  computed: {
    ...mapState(["icon"]),
  },
  methods: {
    ...mapMutations(['changFlagVuex', "reset"]),
    onClickLeft() {
      this.flag = false;
      this.reset({ oldIcon: this.oldIcon })
    },
    onClickRight() {
      this.flag = false;
    },
    changFlag(flag, index) {
      this.changFlagVuex({ flag, index })
    }
  },
};
</script>

<style scoped>
.oul {
  display: flex;
  flex-wrap: wrap;
  text-align: center;
}

.oul li {
  width: 25%;
  position: relative;
}

.oul li span {
  position: absolute;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  text-align: center;
  color: #fff;
  font-size: 12px;
  line-height: 18px;
  top: -10px;
  right: 7px;
}

.close {
  background: #ccc;
}

.add {
  background: skyblue;
}

li i {
  font-size: 26px;
}
</style>